﻿using EnvDTE;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.VisualStudio.Package;
using VSLangProj;

namespace Agile_Planner_Platform.AgilePlanner
{
    [Guid("65374288-21CB-4D47-A211-2D32D6E7586F")]
    public class AgilePlannerProjectNode : ProjectNode
    {
        #region Enum for image list
        internal enum AgilePlannerProjectImageName
        {
            Project = 0,
        }
        #endregion

        #region Constants
        internal const string ProjectTypeName = "AgilePlannerProject";
        #endregion

        #region Fields
        private VsPackage package;
        internal static int imageOffset;
        private static ImageList imageList;
        private VSLangProj.VSProject vsProject;
        #endregion

        #region Constructors
        static AgilePlannerProjectNode()
        {
            imageList = Utilities.GetImageList(typeof(AgilePlannerProjectNode).Assembly.GetManifestResourceStream("Agile_Planner_Platform.Resources.AgilePlannerProjectImageList.bmp"));
        }

        public AgilePlannerProjectNode(VsPackage package)
        {
            this.package = package;

            InitializeImageList();
            InitializeCATIDs();
        }
        #endregion

        #region Properties
        public static ImageList ImageList
        {
            get
            {
                return imageList;
            }
            set
            {
                imageList = value;
            }
        }

        protected internal VSLangProj.VSProject VSProject
        {
            get
            {
                if (vsProject == null)
                {
                    vsProject = new Microsoft.VisualStudio.Package.Automation.OAVSProject(this);
                }

                return vsProject;
            }
        }
        #endregion

        #region Overriden implementation
        public override Guid ProjectGuid
        {
            get { return typeof(AgilePlannerProjectFactory).GUID; }
        }

        public override string ProjectType
        {
            get { return ProjectTypeName; }
        }

        public override int ImageIndex
        {
            get
            {
                return imageOffset + (int)AgilePlannerProjectImageName.Project;
            }
        }

        protected override NodeProperties CreatePropertiesObject()
        {
            return new AgilePlannerProjectNodeProperties(this);
        }

        public override object GetAutomationObject()
        {
            return new OAAgilePlannerProject(this);
        }

        public override FileNode CreateFileNode(ProjectElement item)
        {
            AgilePlannerProjectFileNode node = new AgilePlannerProjectFileNode(this, item);

            node.OleServiceProvider.AddService(typeof(Project), new OleServiceProvider.ServiceCreatorCallback(this.CreateServices), false);
            node.OleServiceProvider.AddService(typeof(ProjectItem), node.ServiceCreator, false);
            node.OleServiceProvider.AddService(typeof(VSProject), new OleServiceProvider.ServiceCreatorCallback(this.CreateServices), false);

            return node;
        }
        #endregion

        #region Private implementation
        private void InitializeImageList()
        {
            imageOffset = this.ImageHandler.ImageList.Images.Count;

            foreach (Image img in ImageList.Images)
            {
                this.ImageHandler.AddImage(img);
            }
        }

        private void InitializeCATIDs()
        {
            this.AddCATIDMapping(typeof(AgilePlannerProjectNodeProperties), typeof(AgilePlannerProjectNodeProperties).GUID);
            this.AddCATIDMapping(typeof(AgilePlannerProjectFileNodeProperties), typeof(AgilePlannerProjectFileNodeProperties).GUID);
        }

        private object CreateServices(Type serviceType)
        {
            object service = null;
            if (typeof(VSLangProj.VSProject) == serviceType)
            {
                service = this.VSProject;
            }
            else if (typeof(EnvDTE.Project) == serviceType)
            {
                service = this.GetAutomationObject();
            }
            return service;
        }
        #endregion
    }
}
